package com.ysm.smfu.food.model;

import android.content.Context;

import com.google.gson.Gson;
import com.ysm.smfu.bean.FoodStore;
import com.ysm.smfu.bean.KeyValueBean;
import com.ysm.smfu.dto.ConfigAreaDTO;
import com.ysm.smfu.dto.ConfigsDTO;
import com.ysm.smfu.dto.ConfigsMessageDTO;
import com.ysm.smfu.protocal.MyStringCallBack;
import com.ysm.smfu.utils.ThreadUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/26.
 */
public class FoodMainModeImp implements FoodMainModel {
    /**
     * 获取附近商户数据
     */
    @Override
    public void getFoodData(CallBack callBack) {
        List<FoodStore> foodStoreList = new ArrayList<>();
        FoodStore foodStore = new FoodStore();
        foodStoreList.add(foodStore);
        foodStoreList.add(foodStore);
        foodStoreList.add(foodStore);
        foodStoreList.add(foodStore);
        foodStoreList.add(foodStore);
        foodStoreList.add(foodStore);
        foodStoreList.add(foodStore);
        foodStoreList.add(foodStore);
        foodStoreList.add(foodStore);
        callBack.onSuccess(foodStoreList);
    }

    /**
     * 获取推荐位数据
     */
    @Override
    public void getRecommedUrls(MyStringCallBack myStringCallBack) {

    }

    @Override
    public void flushData(CallBack callBack) {
        List<FoodStore> foodStoreList = new ArrayList<>();
        FoodStore foodStore = new FoodStore();
        foodStoreList.add(foodStore);
        foodStoreList.add(foodStore);
        callBack.onSuccess(foodStoreList);
    }

    @Override
    public void loadMoreData(CallBack callBack) {
        List<FoodStore> foodStoreList = new ArrayList<>();
        FoodStore foodStore = new FoodStore();
        foodStoreList.add(foodStore);
        foodStoreList.add(foodStore);
        foodStoreList.add(foodStore);
        foodStoreList.add(foodStore);
        foodStoreList.add(foodStore);
        callBack.onSuccess(foodStoreList);
    }


    List<KeyValueBean> mParentLists = new ArrayList<>();
    List<ArrayList<KeyValueBean>> mChildrenListLists = new ArrayList<>();

    @Override
    public void getPopuData(final Context context, final FoodMainModeImp.CallBack callBack) {
        ThreadUtils.runOnBackThread(new Runnable() {
            @Override
            public void run() {
                initData(context, callBack);
            }
        });
    }

    //    智能排序
    List<KeyValueBean> smartData = new ArrayList<>();
    String[] allCategpry = {"全部", "甜点饮品", "生日蛋糕", "火锅", "自助餐", "小吃快餐", "日韩料理", "西餐", "饕餮宴请", "烧烤烤肉", "川湘菜"};
    String[] allCounts = {"1000", "800", "1000", "800", "1000", "800", "1000", "800", "1000", "800", "200"};

    List<KeyValueBean> listAll = new ArrayList<>();

    /**
     * 初始化数据
     */
    private void initData(Context context, FoodMainModeImp.CallBack callBack) {
        smartData.add(new KeyValueBean("", "智能排序"));
        smartData.add(new KeyValueBean("", "由高到低"));
        smartData.add(new KeyValueBean("", "人气最高"));
        for (int i = 0; i < allCategpry.length; i++) {
            listAll.add(new KeyValueBean(allCategpry[i], allCounts[i]));
        }
        getConfigsDatas(context);
        callBack.onGetData(smartData, listAll, mParentLists, mChildrenListLists);
    }


    /**
     * 获取默认数据
     */
    private void getConfigsDatas(Context context) {
        try {
            InputStream is = context.getAssets().open("searchType.json");
            String searchTypeJson = readStream(is);
            Gson gson = new Gson();
            ConfigsMessageDTO messageDTO = gson.fromJson(searchTypeJson, ConfigsMessageDTO.class);
            ConfigsDTO configsDTO = messageDTO.getInfo();

            List<ConfigAreaDTO> configAreaListDTO = configsDTO.getCantonAndCircle();
            for (ConfigAreaDTO configAreaDTO : configAreaListDTO) {
                KeyValueBean keyValueBean = new KeyValueBean();
                keyValueBean.setKey(configAreaDTO.getKey());
                keyValueBean.setValue(configAreaDTO.getValue());
                mParentLists.add(keyValueBean);

                ArrayList<KeyValueBean> childrenLists = new ArrayList<>();
                for (KeyValueBean keyValueBean1 : configAreaDTO.getBusinessCircle()) {
                    childrenLists.add(keyValueBean1);
                }
                mChildrenListLists.add(childrenLists);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String readStream(InputStream is) {
        try {
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            int i = is.read();
            while (i != -1) {
                bo.write(i);
                i = is.read();
            }
            return bo.toString();
        } catch (IOException e) {
            return "";
        }
    }

    public interface CallBack {
        void onSuccess(List<FoodStore> foodStoreList);

        void onFail();

        void onGetData(List<KeyValueBean> smartData, List<KeyValueBean> listAll, List<KeyValueBean> mParentLists, List<ArrayList<KeyValueBean>> mChildrenListLists);
    }
}
